//
//  JWKStatusTableViewCell.swift
//  Weibo
//
//  Created by 姜维克 on 16/5/24.
//  Copyright © 2016年 姜维克. All rights reserved.
//

import UIKit
import SnapKit

class JWKStatusTableViewCell: UITableViewCell {
    
    lazy var originalView : JWKStatusOriginalView = JWKStatusOriginalView()
    lazy var retweetedView : JWKStatusRetweetedView = JWKStatusRetweetedView()
    lazy var statusTabBar : JWKStatusTabBar = JWKStatusTabBar()
    
    
    //约束属性
    var topTabbarConstraint : Constraint?
    
    var statusViewModel : JWKStatusViewModel?{
        didSet{
            originalView.statusViewModel = statusViewModel
            
//            retweetedView.statusViewModel = statusViewModel
            statusTabBar.statusViewModel = statusViewModel
            
            
            //卸载约束
            topTabbarConstraint?.uninstall()
            
            //原创微博
            if statusViewModel?.status?.retweeted_status == nil{
                //设置数据
                retweetedView.statusViewModel = nil
                
                //隐藏转发视图
                retweetedView.hidden = true
                
                //设置约束
                statusTabBar.snp_updateConstraints { (make) in
                    self.topTabbarConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                }
                
                
            }else{
                //设置数据
                retweetedView.statusViewModel = statusViewModel
                
                //显示转发视图
                retweetedView.hidden = false
                
                //设置约束
                statusTabBar.snp_updateConstraints { (make) in
                    self.topTabbarConstraint = make.top.equalTo(retweetedView.snp_bottom).constraint
                    
                }
                
                
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //设置随机颜色cell
        contentView.backgroundColor = UIColor.redColor()

        //设置选中效果为不显示改变
        //        self.selectionStyle = .None
        
        setupUI()
    }
    
    private func setupUI(){
        //cell的视图可以分成四个部分:1.原创微博2.转发微博3.底部工具条4.配图
        
        
        contentView.addSubview(originalView)
        contentView.addSubview(retweetedView)
        contentView.addSubview(statusTabBar)
        
        //设置约束
        originalView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(contentView)
            
        }
        
        retweetedView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.left.right.equalTo(contentView)
        }
        
        statusTabBar.snp_makeConstraints { (make) in
            self.topTabbarConstraint = make.top.equalTo(retweetedView.snp_bottom).constraint
//            make.height.equalTo(36)
            make.left.right.equalTo(contentView)
        }
        
        
        contentView.snp_makeConstraints { (make) in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(statusTabBar)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
